/*
 * Conference Guru
 * ReviewerViewPanel.java
 * version Autumn 2012
 * 
 * Author: Lawrence Grass 
 * Co-Author: Tyler Simerell
 * Code Recycled from Darrion Harris
 * CopyFile method recyled from Tyler Simerell.
 * 
 */
package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import model.ConferencePaper;
import model.Review;
import model.Reviewer;

/*
 * 
 */
@SuppressWarnings("serial")
public class ReviewerViewPanel extends JPanel
{
	Reviewer my_reviewer;
	ArrayList<ConferencePaper> my_papers = new ArrayList<ConferencePaper>();
	JScrollPane my_main_view;
	JPanel my_main_panel = new JPanel();
	final Dimension row_item_size = new Dimension((SPCViewport.WIDTH - 50)/10, SPCViewport.ROW_HEIGHT);
	
	public ReviewerViewPanel(Reviewer the_reviewer)
	{
		super();
		setBackground(BaseViewFrame.BACKGROUND_COLOR);
		my_reviewer = the_reviewer;
	    
		my_papers = my_reviewer.getManuscripts();
		viewPanelSetup();
	}

	// Edited By Darrion Harris
	private void viewPanelSetup()
	{
		//creating a review option for every paper that's assigned to him/her
		if(!my_papers.isEmpty())
		{
			my_main_panel.setLayout(new BoxLayout(my_main_panel, BoxLayout.Y_AXIS));
			my_main_panel.setBackground(BaseViewFrame.BACKGROUND_COLOR);
			JLabel title_label = new JLabel("Papers to review: ");
			JPanel header_panel = new JPanel();
			header_panel.setLayout(new BoxLayout(header_panel, BoxLayout.X_AXIS));
			header_panel.add(title_label);
			header_panel.add(Box.createHorizontalStrut(SPCViewport.WIDTH - 100));
			header_panel.setOpaque(false);
			header_panel.setPreferredSize(new Dimension(WIDTH, 100));
			my_main_panel.add(header_panel);
			for(final ConferencePaper paper: my_papers)
			{
				
				JPanel secondary_panel = new JPanel(new FlowLayout());		
				JLabel paper_name = new JLabel(paper.getTitle());
				my_main_panel.add(new JSeparator());
				
				
				final JTextField file_path_field = new JTextField();
				file_path_field.setEditable(false);

				JLabel file_name = new JLabel("Your File Name: ");
				final JTextField file_name_field = new JTextField(20);
				file_name_field.setEditable(false);
				
				//Sets up the file chooser button.
				JButton file_chooser = new JButton("Choose a File");
				final JButton submit_button = new JButton("Submit Review");
				submit_button.setEnabled(false);
				
				file_chooser.addActionListener(new AbstractAction()
			      {  
					@Override
			        public void actionPerformed(final ActionEvent the_event)
			        {
						JFileChooser chooser = new JFileChooser();
						int userChoice = chooser.showOpenDialog(null);
						if(userChoice == JFileChooser.APPROVE_OPTION)
						{
							File file = chooser.getSelectedFile();
							file_path_field.setText(file.getAbsolutePath());
							file_name_field.setText(file.getName());
							submit_button.setEnabled(true);
						}
			        }
			      });						
				
				
				//Sets up the Submit Review button
				submit_button.addActionListener(new AbstractAction()
				  {
					@Override
					public void actionPerformed(ActionEvent the_event) 
					{
						if(!file_path_field.getText().equals(""))
						{
							File sent_in_file = new File(file_path_field.getText());
							File review_file_path = null;
							if(sent_in_file.exists())
							{
								review_file_path = copyFile(sent_in_file); //does the copy to the Reviews folder
								Review temp_review = new Review(my_reviewer.getUser(), review_file_path);
								temp_review.setReview(sent_in_file);
								paper.setReview(temp_review);
								submit_button.setText("Submitted");
								submit_button.setBackground(Color.GREEN);
								submit_button.setEnabled(false);
								
							}			
						}
						else
						{
							
							file_name_field.setText("You need to select a file first.");
						}
						
					}
				  });
				
				//checks to see if this reviewer has already submitted a review for that paper.
				for(Review rev : paper.getReviews())
				{
					if(rev.isReviewed() && rev.getReviewer().equals(my_reviewer.getUser()))
					{
						submit_button.setEnabled(false);
						submit_button.setBackground(Color.GREEN);
						submit_button.setText("Submitted");
					}
				}
				//sets the sizes for all the components.
				paper_name.setPreferredSize(new Dimension(row_item_size.width * 5, row_item_size.height));
				secondary_panel.add(paper_name);
				
				file_name.setPreferredSize(new Dimension(row_item_size));
				secondary_panel.add(file_name);
				
				file_name_field.setPreferredSize(new Dimension((row_item_size.width - row_item_size.width / 2), row_item_size.height));
				secondary_panel.add(file_name_field);
				
				file_chooser.setPreferredSize(new Dimension(row_item_size));
				secondary_panel.add(file_chooser);
				
				submit_button.setPreferredSize(new Dimension(row_item_size));
				secondary_panel.add(submit_button);

				secondary_panel.setBorder(BorderFactory.createEtchedBorder());
				secondary_panel.setBackground(BaseViewFrame.BACKGROUND_COLOR);
				
				my_main_panel.add(secondary_panel);
				my_main_panel.add(Box.createVerticalStrut(10));
			}			
			my_main_view = new JScrollPane(my_main_panel);
			my_main_view.setBackground(BaseViewFrame.BACKGROUND_COLOR);
			my_main_view.setBorder(BorderFactory.createEmptyBorder());
			add(my_main_view);		
		}
		else
		{
			JLabel no_papers = new JLabel("Sorry, you have no papers assigned to you at this time.");
			my_main_panel.setBackground(BaseViewFrame.BACKGROUND_COLOR);
			my_main_panel.add(no_papers);
			add(my_main_panel);
		}
	}
	

	/*
	 * Method recycled from Tyler
	*This method copies the file into the file directory by using the FileInputStream
	*and the FileOutputStream <Java API is quite helpful here>. 
	*/
	private File copyFile(File the_input_file)
	{
		File newFile = null;
		FileInputStream input;
		try 
		{
			input = new FileInputStream(the_input_file);
			newFile = new File("Reviews/" , the_input_file.getName());
			FileOutputStream output = new FileOutputStream(newFile);
			byte[] stream = new byte[input.available()];
			input.read(stream);
			output.write(stream);
			output.close();
			input.close();
		}
		catch (FileNotFoundException e) 
		{
			System.err.println("File not found. Whoops!");
		} 
		
		catch (IOException e) 
		{
			System.err.println("IO Exception");
		}
		
		return newFile;
	}

}
